﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MangioneManagement.Models;
using System.Data;

namespace MangioneManagement.Controllers
{
    public class ProductController : Controller
    {
        private MangioneManagementDbContext db = new MangioneManagementDbContext();


        #region Index
        //
        // GET: /Product/

        public ActionResult Index()
        {
#if DEBUG == true
            return View(new List<Product> { new Product { ProductId = 1, Brand = "Yamaha", Model = "XMF456", LogicalStock = 2, PhysicalStock = 1 } });
#else
            return View(db.Products.ToList());
#endif
        }

        [HttpPost]
        public ActionResult Index(string submit, string searchCriteria)
        {
            switch (submit)
            {
                case "Buscar Producto":
#if DEBUG == true
                    return View(new List<Product> { new Product { ProductId = 1, Brand = "Yamaha", Model = "XMF456", LogicalStock = 2, PhysicalStock = 1 } });
#else
                    var query = from p in db.Products
                                where (((String.IsNullOrEmpty(p.Brand) == false) ? p.Brand : String.Empty) +
                                                            ((String.IsNullOrEmpty(p.Model) == false) ? p.Model : String.Empty) 
                                                         ).Contains(searchCriteria)
                                select p;

                    return View(query.AsEnumerable().ToList());
#endif
                case "Hacer Pedidos de Productos":
                    return RedirectToAction("Buy");

                case "Pedidos Realizados":
                    return RedirectToAction("BuysOnTheFly");

                case "Agregar Producto":
                    return RedirectToAction("Create");

                default:
                    return RedirectToAction("Create");
            }

        }

        #endregion
    

        [HttpPost]
        public ActionResult Edit(Product product)
        {
#if DEBUG == true
            return RedirectToAction("Index");
#endif
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(product);
        }

        //
        // GET: /Product/BuysOnTheFly

        public ActionResult BuysOnTheFly()
        {
            return View();
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Product prod)
        {
#if DEBUG == true
            return RedirectToAction("Index");
#else
            if (ModelState.IsValid)
            {
                db.Products.Add(prod);
                db.SaveChanges();
                return RedirectToAction("Index");
            }


            return View(prod);
#endif
        }


        //
        // GET: /Product/Edit

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // GET: /Product/Delete

        public ActionResult Delete(int id)
        {
#if DEBUG == true
            return View(new Product
            {
               Brand = "Yamaha",
               LogicalStock = 2,
               Model = "XBV456",
               PhysicalStock = 1,
               ProductId = 1

            });
#else
            Product product = db.Products.Find(id);
            return View(product);
#endif
        }

        //
        // POST: /Product/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
#if DEBUG == true
            return RedirectToAction("Index");
#else
            Product product = db.Products.Find(id);
            db.Products.Remove(product);
            db.SaveChanges();
            return RedirectToAction("Index");
#endif
        }

        //
        // GET: /Product/Buy

        public ActionResult Buy()
        {
            return View();
        }


    }
}
